// config.ts
import Cache from '@/utils/cache'

interface Config {
  version: string
  baseURL: string
  baseImgURL: string
}

// 环境变量类型声明
declare let process: {
  env: {
    VITE_ENV: 'development' | 'production'
    VITE_APP_MINAPP: string
  }
}

let production: string = import.meta.env.VITE_APP_MINAPP

// 处理 H5 环境
// #ifdef H5
production = window.location.origin
// #endif

let envVersion: any = 'develop'

// 处理微信小程序环境
// #ifdef MP-WEIXIN
const accountInfo = uni.getAccountInfoSync()
envVersion = accountInfo.miniProgram.envVersion
// #endif

// 非 H5 环境处理
// #ifndef H5
switch (envVersion) {
  case "develop": // 开发版
    production = import.meta.env.VITE_APP_MINAPP
    break;
  case "trial":   // 体验版
    production = import.meta.env.VITE_APP_MINAPP
    break;
  case "release": // 正式版
    production = import.meta.env.VITE_APP_MINAPP
    break;
  default:
    // const check: never = envVersion
    break
}
// #endif


const baseURLMap: Record<string, string> = {
  development: import.meta.env.VITE_APP_MINAPP,
  production: production,
}

let baseURL: string = baseURLMap[import.meta.env.VITE_ENV]

// 开发环境或 debug 模式处理
if (import.meta.env.VITE_ENV === 'development' || (typeof location !== 'undefined' && location.href.includes('debug'))) {
  const settingUrl = Cache.get<string>('BASEURL')
  if (settingUrl) {
    baseURL = settingUrl
  }
}

const baseImgURL: string = 'https://packer-data-files.oss-cn-shenzhen.aliyuncs.com/mapp_static/'

const config: Config = {
  version: '1.0.0',
  baseURL,
  baseImgURL
}

export default config